﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Meraz.Core.Model;
using System.Reflection;
using Meraz.Core.Helpers;

namespace Meraz.Core.Repositories
{
    public interface IDomainRepository : IRepository<Domain, int>
    {
        Domain Get(string domainName);
        List<Domain> Page(int pageSize, int pageIndex);
        List<Domain> Page(int pageSize, int pageIndex, string fieldName);
    }
    public class DomainRepository : EFRepositoryBase<Domain, int>, IDomainRepository
    {
        public DomainRepository() : base() { }
        public DomainRepository(Data.IMerazContext context) : base(context) { }
        public override Domain GetById(int id)
        {
            return context.Domains
                .Where(where1 => where1.ID == id)
                .FirstOrDefault();
        }
        public Domain Get(string domainName)
        {
            return context.Domains
                .Where(where1 => where1.Name == domainName)
                .FirstOrDefault();
        }

        public List<Domain> Page(int pageSize, int pageIndex)
        {
            return Page(pageSize, pageIndex, "Name");
        }

        public List<Domain> Page(int pageSize, int pageIndex, string fieldName)
        {
            var query = context.Domains.OrderBy(Util.GetField<Domain>(fieldName))
                .Skip(pageIndex * pageSize).Take(pageSize);
            return query.ToList();
        }

        public override void Add(Domain entity)
        {
            context.Domains.AddObject(entity);
        }

        public override void Delete(Domain entity)
        {
            context.Domains.DeleteObject(entity);
        }

        public override List<Domain> All()
        {
            return context.Domains.ToList();
        }
    }
}
